package com.vanysoft.business.system.license.controller;

import com.vanysoft.business.system.license.controller.demo.SystemLicenseInfo;
import com.vanysoft.business.system.license.entity.TSystemLicense;
import com.vanysoft.business.system.license.service.LicenseService;
import com.vanysoft.framework.aop.VanySoftValidation;
import com.vanysoft.framework.base.action.BaseController;
import com.vanysoft.framework.exception.VanySoftBaseException;
import com.vanysoft.framework.messagesource.MessageCodeConstant;
import com.vanysoft.framework.utils.JSONResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * 类名称：LicenseAction
 * 类描述：登录、注销
 * 创建人：X
 * 创建时间：2017年9月18日下午11:00:21
 * 修改人：X
 * 修改时间：2017年9月18日
 * 版本信息：@version 1.0
 * Copyright (c) 2017SharpClaw-版权所有
 * 修改备注：
 */
@RestController
@RequestMapping("/system/license")
public class LicenseAction extends BaseController {

    @Autowired
    private LicenseService licenseService;


    @RequestMapping(value = "/decrypted", method = RequestMethod.POST)
    public JSONResponse decryptedLicense(HttpServletRequest request, @Valid TSystemLicense inputObj, BindingResult result) throws VanySoftBaseException {
        //String[] str = licenseService.getCustomInfo(inputObj);
        //String customInfo = startDate + ":true:" + customer + ":" + productCode + ":" + endDate;
        //String msg = "";
        //String status = "";//0:试用版本;1:正常版本;2:快到期;3:异常
        //Map<String, Object> map = new HashMap<String, Object>();
        //map.put("msg", msg);
        //map.put("status", status);
        SystemLicenseInfo obj = licenseService.getSystemLicenseInfo(inputObj);
        return new JSONResponse(MessageCodeConstant.RTN_CODE_SUCCESS, obj);
    }

    /**
     * Function Name               queryLicense
     *
     * @return
     * @throws VanySoftBaseException
     * @description 查询注册码信息
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年9月18日下午11:41:33          KING          Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public JSONResponse queryLicense(HttpServletRequest request) throws VanySoftBaseException {
        TSystemLicense obj = licenseService.queryTopLicense();
        return new JSONResponse(MessageCodeConstant.RTN_CODE_SUCCESS, obj);
    }

    /**
     * Function Name               checkLicense
     *
     * @return
     * @throws VanySoftBaseException
     * @description 校验注册码是否正确
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年9月18日下午11:36:53          X          Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/checkLicense", method = RequestMethod.POST)
    public JSONResponse checkLicense(HttpServletRequest request, @Valid TSystemLicense inputObj, BindingResult result) throws VanySoftBaseException {
        Map<String, Object> map = new HashMap<String, Object>();
        boolean b = licenseService.checkLicense(inputObj);
        String isOk = "NO";
        if (b) isOk = "OK";
        map.put("isOk", isOk);
        return new JSONResponse(MessageCodeConstant.RTN_CODE_SUCCESS, map);
    }

    /**
     * Function Name               updateLicense
     *
     * @return
     * @throws VanySoftBaseException
     * @description 修改注册码
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年9月18日下午11:38:41          X          Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @VanySoftValidation
    public JSONResponse updateLicense(HttpServletRequest request, @Valid TSystemLicense inputObj, BindingResult result) throws VanySoftBaseException {
        licenseService.updateLicense(inputObj);
        return new JSONResponse();
    }


}
